home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-15 | 11.0 KB | 520 lines | [TEXT/CWIE] |
- /*
-
- File: UDialog.cp
- Project: Sprocket Framework 1.1 (DR2), released 6/15/96
- Contains: Dialog utilities
- To Do: ?
-
- Sprocket Major Contributors:
- ----------------------------
- Dave Falkenburg, producer of Sprocket 1.0
- Bill Hayden, producer of Sprocket 1.1
- Steve Sisak, producer of the upcoming Sprocket 2.0
-
- Pete Alexander Steve Falkenburg Randy Thelen
- Eric Berdahl Nitin Ganatra Chris K. Thomas
- Marshall Clow Dave Hershey Leonard Rosenthal
- Tim Craycroft Dave Mark Dean Yu
- David denBoer Gary Powell
- Cameron Esfahani Jon Summers Apple Computer, Inc.
-
- Comments, Additions, or Corrections:
- ------------------------------------
- Bill Hayden, Nikol Software <nikol@codewell.com>
-
- */
-
- #include "Sprocket.h"
- #include "UDialog.h"
- #include "UString.h"
-
- #ifndef __PROCESSES__
- #include <Processes.h>
- #endif
-
- #ifndef __THREADS__
- #include <Threads.h> // For YieldToAnyThread()
- #endif
-
-
- #ifndef __STANDARDFILE__
- #include <StandardFile.h> // For ModalFilterYDProcPtr
- #endif
-
- // Some types which should probably be defined in <Dialogs.h>
- // NOTE: These must be aligned on 2-byte boundaries
-
- #if PRAGMA_ALIGN_SUPPORTED
- #pragma push
- #pragma options align=mac68k
- #endif
-
- struct DialogItem
- {
- long usedByDialogManager;
- Rect boundsRect;
- char type;
- char length;
- };
-
- struct DialogItemList // a.k.a. a 'DITL'
- {
- short count;
- DialogItem firstItem[1];
- };
-
- // Restore default alignment
-
- #if PRAGMA_ALIGN_SUPPORTED
- #pragma pop
- #endif
-
-
- typedef DialogItem *DialogItemPtr;
- typedef DialogItemList **DialogItemListHandle;
- typedef DialogTemplate **DialogTemplateHandle;
-
-
- // private function Prototypes
-
- pascal Boolean StandardDialogFilterProc(DialogRef theDialog, EventRecord * anEvent, short * itemHit);
- static pascal Boolean StandardDialogFilterYDProc(DialogRef theDialog, EventRecord * anEvent, short * itemHit, void * yourData);
- static pascal Boolean StandardCloseDialogFilterProc(DialogRef theDialog, EventRecord * anEvent, short * itemHit);
- static Boolean FilterProcCommon(DialogRef theDialog, EventRecord * anEvent, short * itemHit);
-
-
-
- ///////////////////////////////////////////////////////////
- //
- // StandardAlert
- //
- // An alternative to Alert() which uses the extended
- // Dialog Manager capabilities.
- //
- // I’m not sure we really need this call, but it seems
- // to do the trick just fine.
-
- short
- StandardAlert( short dlogID,
- short defaultItem, /* = ok */
- short cancelItem, /* = 0 */
- ModalFilterUPP customFilterUPP /* = nil */)
- {
- DialogRef theDialog;
- short itemHit = 0;
- ModalFilterUPP filterToUse;
-
- HiliteWindowsForModalDialog(false);
-
- theDialog = GetNewDialog(dlogID,nil,(WindowRef) -1);
- if (defaultItem)
- SetDialogDefaultItem(theDialog,defaultItem);
- if (cancelItem)
- SetDialogCancelItem(theDialog,cancelItem);
-
- if (customFilterUPP)
- filterToUse = customFilterUPP;
- else
- filterToUse = StandardDialogFilterUPP;
-
- do
- ModalDialog(filterToUse,&itemHit);
- while (itemHit == 0);
-
- DisposeDialog(theDialog);
-
- HiliteWindowsForModalDialog(true);
-
- return itemHit;
- }
-
-
-
-
- /*****************************************************************************/
-
-
-
- ///////////////////////////////////////////////////////////
- //
- // ErrorAlert
- //
- // A nice error reporting routine which presents an
- // alert box containing the supplied text.
- //
-
- void ErrorAlert(short stringList, short whichString, Boolean fatalError)
- {
- Str255 errorString;
- const StringPtr nullStr = (StringPtr) "\p";
-
-
- GetIndString(errorString, stringList, whichString);
- SetCursor(&qd.arrow);
- ParamText(errorString, nullStr, nullStr, nullStr);
-
- (void) StandardAlert(kErrorAlertID);
-
- if (fatalError)
- ExitToShell();
- }
-
-
-
- /*****************************************************************************/
-
-
-
- ///////////////////////////////////////////////////////////
- //
- // ErrorReporter
- //
- // A nice error reporting routine which presents an
- // alert box containing the supplied the error, as well
- // as the file and line it occurred on.
- //
-
- void ErrorReporter(OSErr err, char* file, long line)
- {
- Str255 errStr, lineStr;
- char filename[255];
-
- NumToString(err, errStr);
- NumToString(line, lineStr);
- ccpy(filename, file);
- c2p(filename);
-
- #if qDebug
- //SysBreakStr("\pError State: ErrorReporter Called");
- #endif
-
- ParamText(errStr, (StringPtr)filename, lineStr, "\p");
-
- SetCursor(&qd.arrow);
- StandardAlert(kErrorReporterAlertID);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- ///////////////////////////////////////////////////////////
- //
- // StandardDialogFilter and StandardDialogFilterYD
- //
- // These function takes care of routing events not meant
- // for the dialog window to other parts of the application.
- //
- // Use them as an alternative to passing a NIL ModalFilterProc
- // to ModalDialog() and CustomGet(Put)File. Unlike the default
- // filter, these routines properly processes update events
- // to keep background processes running.
- //
- // The Thread Manager, if present, is also called to yield
- // control to other cooperative threads within the process.
- //
- // Because of pascal calling conventions we need two separate
- // routines, but this is minimized by sharing implementation
- // in FilterProcCommon.
-
-
- ModalFilterUPP StandardDialogFilterUPP
- = NewModalFilterProc(StandardDialogFilterProc);
-
- ModalFilterYDUPP StandardDialogFilterYDUPP
- = NewModalFilterYDProc(StandardDialogFilterYDProc);
-
-
- /*****************************************************************************/
-
-
-
- pascal Boolean StandardDialogFilterProc(DialogRef theDialog, EventRecord* anEvent, short* itemHit)
- {
- // Call through common code to check for events we’d like to handle.
- // If that is unsuccessful, call through the System 7 StdFilterProc
- // to handle CR, “CMD-.” & ESC in an international-friendly manner.
-
- if (FilterProcCommon(theDialog, anEvent, itemHit))
- return true;
- else
- return (StdFilterProc(theDialog, anEvent, itemHit));
- }
-
-
- /*****************************************************************************/
-
-
- pascal Boolean StandardDialogFilterYDProc(DialogRef theDialog, EventRecord* anEvent, short* itemHit, void * /*unusedData*/)
- {
- // We don’t call through to StdFilterProc since the
- // Standard File Package already does everything we need.
-
- return FilterProcCommon(theDialog, anEvent, itemHit);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- void PseudoClickInDialogItem(DialogRef theDialog, short itemToClick)
- {
- ControlRef itemHandle;
- Rect itemBox;
- long finalTicks;
- short itemType;
-
- GetDialogItem(theDialog, itemToClick, &itemType, (Handle *) &itemHandle, &itemBox);
-
- HiliteControl( itemHandle, kControlButtonPart );
- Delay(8, &finalTicks);
- HiliteControl( itemHandle, 0 );
- }
-
-
- /*****************************************************************************/
-
-
-
- Boolean ToggleCheckBox(DialogRef theDialog, short theCheckBox)
- {
- ControlRef itemHandle;
- Rect itemBox;
- short itemType;
-
-
- GetDialogItem(theDialog, theCheckBox, &itemType, (Handle *) &itemHandle, &itemBox);
-
- #if qDebug
- if (itemType < 4 || itemType > 7)
- {
- DebugMessage("\pToggleCheckBox called with non-togglable item");
- return false;
- }
- #endif
-
- if (GetControlValue(itemHandle) == 1)
- {
- SetControlValue(itemHandle, 0);
- return false;
- }
- else
- {
- SetControlValue(itemHandle, 1);
- return true;
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
-
- void SetControlActive(DialogRef theDialog, short itemHit, Boolean hilite)
- {
- short iType;
- Handle iHandle;
- Rect iRect;
-
- GetDialogItem(theDialog, itemHit, &iType, &iHandle, &iRect);
- HiliteControl( (ControlRef)iHandle, hilite ? 0 : 255 );
- }
-
-
-
- /*****************************************************************************/
-
-
-
-
- short GetControlSetting(DialogRef theDialog, short itemHit)
- {
- short iType;
- ControlRef iHandle;
- Rect iRect;
-
- GetDialogItem(theDialog, itemHit, &iType, (Handle *)&iHandle, &iRect);
- return GetControlValue(iHandle);
- }
-
-
-
- /*****************************************************************************/
-
-
-
-
- void SetControlSetting(DialogRef theDialog, short itemHit, short setting)
- {
- short iType;
- ControlRef iHandle;
- Rect iRect;
-
- GetDialogItem(theDialog, itemHit, &iType, (Handle *)&iHandle, &iRect);
- SetControlValue(iHandle, setting);
- }
-
-
-
- /*****************************************************************************/
-
-
-
-
- void GetControlRange(DialogRef theDialog, short itemHit, short *min, short *max)
- {
- short iType;
- ControlRef iHandle;
- Rect iRect;
-
- GetDialogItem(theDialog, itemHit, &iType, (Handle *)&iHandle, &iRect);
- *min = GetControlMinimum(iHandle);
- *max = GetControlMaximum(iHandle);
- }
-
-
-
- /*****************************************************************************/
-
-
-
-
- void SetControlRange(DialogRef theDialog, short itemHit, short min, short max)
- {
- short iType;
- ControlRef iHandle;
- Rect iRect;
-
- GetDialogItem(theDialog, itemHit, &iType, (Handle *)&iHandle, &iRect);
- SetControlMinimum(iHandle, min);
- SetControlMaximum(iHandle, max);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- void SetEditText(DialogRef theDialog, short itemHit, ConstStr255Param textStr)
- {
- short iType;
- Handle iHandle;
- Rect iRect;
-
-
- GetDialogItem(theDialog, itemHit, &iType, &iHandle, &iRect);
-
- #if qDebug
- if (iType != statText && iType != editText)
- {
- DebugMessage("\pSetEditText called with non-text item");
- return;
- }
- #endif
-
- SetDialogItemText( iHandle, textStr );
- }
-
-
-
- /*****************************************************************************/
-
-
-
- void GetEditText(DialogRef theDialog, short itemHit, StringPtr textStr)
- {
- short iType;
- Handle iHandle;
- Rect iRect;
-
-
- GetDialogItem(theDialog, itemHit, &iType, &iHandle, &iRect);
-
- #if qDebug
- if (iType != statText && iType != editText)
- {
- DebugMessage("\pGetEditText called with non-text item");
- return;
- }
- #endif
-
- GetDialogItemText( iHandle, textStr );
- }
-
-
-
- /*****************************************************************************/
-
-
-
- Boolean FilterProcCommon(DialogRef theDialog, EventRecord * anEvent, short * /* itemHit */)
- {
- switch (anEvent->what)
- {
- case updateEvt:
- case activateEvt:
- // Update or activate for the dialog window?
-
- if (theDialog == (DialogRef) anEvent->message)
- break;
-
- // no, fall through to HandleEvent
-
- case diskEvt:
- HandleEvent(anEvent);
- return(false);
-
- default:
- break;
- }
-
- if (gHasThreadManager) // If we have threads, let them run!
- YieldToAnyThread();
-
- return false; // We didn’t handle the event
- }
-
-
-
- /*****************************************************************************/
-
-
-
- void SetDialogFontAndSize ( DialogRef theDialog, short fontNum, short fontSize )
- {
- GrafPtr savePort;
- TEHandle teh = GetDialogTextEdit ( theDialog );
-
- GetPort ( &savePort );
- SetGrafPortOfDialog ( theDialog );
-
- // set up the port info
-
- TextFont ( fontNum );
- TextSize ( fontSize );
-
- // now deal with the static & edit text issues
-
- if ( teh != NULL )
- {
- FontInfo f;
- TEPtr pText;
-
- GetFontInfo ( &f );
- pText = *teh;
- pText->txFont = applFont;
- pText->txSize = fontSize;
- pText->lineHeight = f.ascent + f.descent + f.leading;
- pText->fontAscent = f.ascent;
- }
-
- SetPort ( savePort );
- }
-